<table cellpadding="0" cellspacing="0">
	<tbody>
        <tr>
            <td class="leftside">
           		<?php require_once("menu.php");?>
            </td>
            <td class="rightside">
                <div class="box" style="margin-left:10px; margin-right: 10px; float:left;">
                	<div id='title_menu' class="ui-corner-top">จัดการหมวดหมู่ประกาศ</div>  
                    <div class="content" style="min-height: 150px;">
                    <?php if($_SESSION['level'] == 'superadministrator'){?>
                    <?php 
					if(!isset($_GET['edit'])){
						$get_last_cat_record = mysql_query('SELECT MAX(cat_sort) AS last_cat FROM category WHERE cat_id != 99');
						$last_cat_id = mysql_fetch_assoc($get_last_cat_record);
					}else{
						$get_cat_sql = 'SELECT * FROM category WHERE cat_id='.$_GET['edit'];	
						$get_cat	 = mysql_query($get_cat_sql);
						$cat		 = mysql_fetch_assoc($get_cat);
					}
					?>
                	  <table border="0" cellspacing="10">
                        <tbody>
                       		<tr>
                            	<td width="80" align="right"><strong>ชื่อหมวดหมู่:</strong></td>
                                <td colspan="4" align="left">
                                	<input type="text" id="cat_name" value="<?php echo $cat['cat_name'];?>"/>
                                </td>
                            </tr>
                            <tr>
                            	<td align="right"><strong>ลำดับ:</strong></td>
                                <td colspan="4" align="left">
                                <?php if(!isset($_GET['edit'])){ ?>
                                	<input type="text" id="cat_sort" value="<?php echo $last_cat_id['last_cat']+1;?>" disabled="disabled"/>
                               	<?php }else{ ?>
									<input type="text" id="cat_sort" value="<?php echo $_GET['edit'];?>" disabled="disabled"/>					
								<?php } ?>
                              	</td>
                            </tr> 
                            <tr>
                            	<td align="right"><strong>สถานะ:</strong></td>
                                <td align="left" width="10">
									<input type="radio" name="status" value="1" <?php echo (isset($_GET['edit']) && $cat['cat_active']==1)?'checked="checked"':'';?>/>
                               	</td>
                                <td align="left" width="50"><strong>ทำงาน</strong></td>
                                <td align="left" width="10"><input type="radio" name="status" value="0" <?php echo (!isset($_GET['edit']))?'checked="checked"':(isset($_GET['edit']) && $cat['cat_active']==0)?'checked="checked"':'';?>/></td>
                                <td align="left"><strong>ไม่ทำงาน</strong></td>
                            </tr> 
                            <tr>
                            	<td align="right" valign="top"><strong>รูปภาพ:</strong></td>
                                <td colspan="4" align="left">
                                <div id="upload" ><span>อัพโหลดรูป</span></div><span id="status" ></span>
                                </td>
                            </tr>
                            <tr>
                            	<td></td>
                                <td colspan="4" align="left">
                                <ul id="cat_img">
                                <?php
								if(isset($_SESSION['cat_img'])){
									$img = $_SESSION['cat_img'];
									echo '<li><img src="../styles/images/category/'.$img.'" filename="'.$img.'"/></li>';
								}elseif(isset($_GET['edit'])){
									$img = $cat['cat_img'];
									echo '<li><img src="../styles/images/category/'.$img.'" filename="'.$img.'"/></li>';
								}
								?>
                                </ul>
                                </td>
                            </tr>
                            <tr>
                            	<td></td>
                                <td colspan="4" align="left">
                                <?php if(!isset($_GET['edit'])){ ?>
                                	<input type="button" id="catsubmit" value="เพิ่มหมวดหมู่" /><span id="loading"></span>
                               	<?php }else{ ?>
                                	<input type="hidden" id="update" value="1" />
                                	<input type="button" id="catsubmit" value="อัพเดทหมวดหมู่" /><span id="loading"></span>
                                <?php } ?>
                                </td>
                            </tr> 
                        </tbody>
                      </table>
                      <?php }?>
                  </div>
                </div>
                
                <div class="box" style="margin-left:10px; margin-right: 10px; float:left;">
                	<div id='title_menu' class="ui-corner-top">หมวดหมู่ประกาศ</div>  
                    <div class="content" style="min-height: 150px;">
                    <?php
					if(isset($_GET['up']) && ($_GET['up'] != 1)){
						mysql_query('UPDATE category SET cat_sort='.$_GET['up'].' WHERE cat_sort='.($_GET['up']-1));
						mysql_query('UPDATE category SET cat_sort='.($_GET['up']-1).' WHERE cat_id='.$_GET['id']);	
					}elseif(isset($_GET['down'])){
						mysql_query('UPDATE category SET cat_sort='.$_GET['down'].' WHERE cat_sort='.($_GET['down']+1));
						mysql_query('UPDATE category SET cat_sort='.($_GET['down']+1).' WHERE cat_id='.$_GET['id']);						
					}
					
					if(isset($_GET['status'])){
						mysql_query('UPDATE category SET cat_active='.$_GET['status'].' WHERE cat_id='.$_GET['id']);
					}
					
                    	$sql = 'SELECT * FROM category ORDER BY cat_sort';
						$q = $db->select_query($sql);
					?>
                	  <table class="list">
                        <thead>
                          <tr align="center">
                            <td colspan="3">ลำดับ</td>
                            <td	width="80">รูปภาพ</td>
                            <td width="400">ชื่อหมวดหมู่</td>
                            <td width="64">สถานะ</td>
                            <td width="60">ลบ</td>    
                            <td width="60">แก้ไข</td>    
                          </tr>
                        </thead>
                        <tbody>
                        <?php
                        while($rs = $db->fetch($q)){
							if($rs['cat_id']!=99){
						?>
                          <tr align="center">
                          	<td><input type="text" id="cat_sort" value="<?php echo $rs['cat_sort'];?>" size="3" disabled="disabled" /></td>
                            <td><a href="?m=posts&p=all&id=<?php echo $rs['cat_id'];?>&up=<?php echo $rs['cat_sort'];?>"><img src="images/arrow_up.jpg" title="เลื่อนขึ้น"/></a></td>
                            <td><a href="?m=posts&p=all&id=<?php echo $rs['cat_id'];?>&down=<?php echo $rs['cat_sort'];?>"><img src="images/arrow_down.jpg" title="เลื่อนลง"/></a></td>
                            <td>
                            <?php if($rs['cat_img'] != ''){?>
                           	  <img src="../styles/images/category/<?php echo $rs['cat_img'];?>" title="<?php echo $rs['cat_name'];?>" />
                           	<?php }else{ 
								echo '<img src="../styles/images/no_pic.gif" title="ทุกหมวด" width="48" />';
								  } 
							?>
                           	</td>
                            <td><strong><a href="?m=posts&p=subcat&cid=<?php echo $rs['cat_id'];?>"><?php echo (!empty($rs['cat_name'])?$rs['cat_name']:'ทุกหมวด');?></a></strong></td>
                            <td>
                            <?php ($rs['cat_active']==1)? 
									print '<a href="?m=posts&p=all&id='.$rs['cat_id'].'&status=0"><strong style="color:green;">ทำงาน</strong></a>' : 
									print '<a href="?m=posts&p=all&id='.$rs['cat_id'].'&status=1"><strong style="color:red;">ไม่ทำงาน</strong></a>';?>
                            </td>
                            <td>
                            <?php
                            if(isset($_SESSION['level']) && ($_SESSION['level'] == 'superadministrator')){
							?>
                            <a href="#" id="delcategory" cid="<?php echo $rs['cat_id'];?>"><img src="images/delete.png" title="ลบหมวดหมู่นี้"/></a>
                            <?php }else{ ?>
                 			<a href="#" onclick="alert('คุณไม่มีสิทธิ์ในการลบหมวดหมู่ใดๆ ทั้งนั้น!!!');return false;" ><img src="images/delete.png" title="ลบหมวดหมู่นี้"/></a>           
                            <?php } ?>
                            </td>
                            <td>
                            <?php
                            if(isset($_SESSION['level']) && ($_SESSION['level'] == 'superadministrator')){
							?>
                            <a href="?m=posts&p=all&edit=<?php echo $rs['cat_id'];?>"><img src="images/edit.png"/></a>
                            <?php }else{ ?>
                 			<a href="#" onclick="alert('คุณไม่มีสิทธิ์ในการลบหมวดหมู่ใดๆ ทั้งนั้น!!!');return false;" ><img src="images/delete.png" title="ลบหมวดหมู่นี้"/></a>           
                            <?php } ?>
                            </td>
                          </tr>  
                          <?php
							}
						}
						  ?>
                        </tbody>
                      </table>
                  </div>
                </div>            
            </td>
        </tr>
   	</tbody>
</table>
<script type="text/javascript">
$(function(){
	var btnUpload=$('#upload');
	var status=$('#status');
	new AjaxUpload(btnUpload, {
		action: 'upload-image.php?upload_cat_img=true',
		name: 'uploadfile',
		onSubmit: function(file, ext){			
			if (!(ext && /^(jpg|png|jpeg|gif)$/.test(ext))){ 
				// extension is not allowed 
				status.text('ไฟล์ที่คุณสามารถอัพโหลดได้คือ .jpg, .jpeg, .png และ .gif เท่านั้น');
				return false;
			}
			status.text('Uploading...');
		},
		onComplete: function(file, response){
			//On completion clear the status
			status.text('');
			//Add uploaded file to list
			var imgstatus = response.split(',');
			
			if(imgstatus[0]==="success"){
				$('#cat_img li').remove();
				$('<li></li>').appendTo('#cat_img').html('<img src="../styles/images/category/'+imgstatus[1]+'" alt="'+imgstatus[1]+'" />').addClass('success');
				count_img++;
				
			} else{
				$('<li></li>').appendTo('#cat_img').text(imgstatus[1]).addClass('error');
			}

		}
	});
});
</script>